package com.genesis.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.genesis.mall.common.CustomException;
import com.genesis.mall.common.PmsNewProductPage;
import com.genesis.mall.common.Result;
import com.genesis.mall.entity.SmsHomeNewProduct;
import com.genesis.mall.service.ISmsHomeNewProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 新鲜好物表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
@RestController
@RequestMapping("/home/newProduct")
@Slf4j
public class SmsHomeNewProductController {
    @Autowired
    private ISmsHomeNewProductService smsHomeNewProductService;


    /**
     * 新品展示
     * @param pageNum
     * @param pageSize
     * @param productName
     * @param recommendStatus
     * @return
     */
    @GetMapping("/list")
    public Result<PmsNewProductPage<SmsHomeNewProduct>> newProduct(Long pageNum, Long pageSize, String productName, Integer recommendStatus){
        Page<SmsHomeNewProduct> pageInfo= new Page(pageNum,pageSize);
        LambdaQueryWrapper<SmsHomeNewProduct> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(StringUtils.isNotEmpty(productName),SmsHomeNewProduct::getProductName,productName);
        queryWrapper.eq(recommendStatus!=null,SmsHomeNewProduct::getRecommendStatus,recommendStatus);
        queryWrapper.orderByDesc(SmsHomeNewProduct::getSort);
        smsHomeNewProductService.page(pageInfo,queryWrapper);

        PmsNewProductPage<SmsHomeNewProduct> productPage = new PmsNewProductPage();
        BeanUtils.copyProperties(pageInfo,productPage);


        return Result.success(productPage);
    }


    /**
     * 修改排序
     * @param sort
     * @param id
     * @return
     */
    @PostMapping("/update/sort/{id}")
    public Result<String>update(@RequestParam String sort ,Long id){
        LambdaUpdateWrapper<SmsHomeNewProduct> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SmsHomeNewProduct::getId,id);
        updateWrapper.set(SmsHomeNewProduct::getSort,sort);
        smsHomeNewProductService.update(updateWrapper);
        return Result.success("修改排序成功");
    }

    /**
     * 新品新增
     * @param list
     * @return
     */
    @PostMapping("/create")
    public Result<String>create(@RequestBody List<SmsHomeNewProduct>list){
        for (SmsHomeNewProduct smsHomeNewProduct : list) {
            LambdaQueryWrapper<SmsHomeNewProduct> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SmsHomeNewProduct::getProductId,smsHomeNewProduct.getProductId());
            int count = smsHomeNewProductService.count(wrapper);
            if (count>0){
                throw new CustomException("添加的新品已存在,不可重复添加!");
            }
            smsHomeNewProductService.save(smsHomeNewProduct);
        }
        return Result.success("添加成功");

    }

    /**
     * 新品删除
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public Result<String>delete(@RequestParam List<Integer> ids){
        log.info("ids",ids);

        smsHomeNewProductService.removeByIds(ids);

        return Result.success("删除成功");
    }

    @PostMapping("/update/recommendStatus")
    public Result<String>updateStatus(@RequestParam List<Integer>ids ,Integer recommendStatus){
        for (Integer id : ids) {
            LambdaUpdateWrapper<SmsHomeNewProduct> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SmsHomeNewProduct::getId,id);
            updateWrapper.set(SmsHomeNewProduct::getRecommendStatus,recommendStatus);
            smsHomeNewProductService.update(updateWrapper);
        }
        return Result.success("修改成功");

    }


}
